﻿using System;
using System.IO;
using UnityEngine;

namespace SEXY
{
	// Token: 0x020002A7 RID: 679
	public abstract class CharFile
	{
		// Token: 0x0600112B RID: 4395 RVA: 0x0006E3D0 File Offset: 0x0006C5D0
		public CharFile(string fileMarkName, string fileDirectory)
		{
			this.CharaFileMark = fileMarkName;
			this.CharaFileDirectory = fileDirectory;
			this.previewInfo = new CharFileInfoPreview();
			this.customInfo = null;
			this.coordinateInfo = null;
			this.clothesInfo = null;
		}

		// Token: 0x1700019F RID: 415
		// (get) Token: 0x0600112C RID: 4396 RVA: 0x0006E440 File Offset: 0x0006C640
		// (set) Token: 0x0600112D RID: 4397 RVA: 0x0006E448 File Offset: 0x0006C648
		public CharFileInfoPreview previewInfo { get; protected set; }

		// Token: 0x170001A0 RID: 416
		// (get) Token: 0x0600112E RID: 4398 RVA: 0x0006E454 File Offset: 0x0006C654
		// (set) Token: 0x0600112F RID: 4399 RVA: 0x0006E45C File Offset: 0x0006C65C
		public CharFileInfoCustom customInfo { get; protected set; }

		// Token: 0x170001A1 RID: 417
		// (get) Token: 0x06001130 RID: 4400 RVA: 0x0006E468 File Offset: 0x0006C668
		// (set) Token: 0x06001131 RID: 4401 RVA: 0x0006E470 File Offset: 0x0006C670
		public CharFileInfoCoordinate coordinateInfo { get; protected set; }

		// Token: 0x170001A2 RID: 418
		// (get) Token: 0x06001132 RID: 4402 RVA: 0x0006E47C File Offset: 0x0006C67C
		// (set) Token: 0x06001133 RID: 4403 RVA: 0x0006E484 File Offset: 0x0006C684
		public CharFileInfoClothes clothesInfo { get; protected set; }

		// Token: 0x06001134 RID: 4404 RVA: 0x0006E490 File Offset: 0x0006C690
		public bool SetClothesInfo(CharFileInfoClothes info)
		{
			return this.clothesInfo.Copy(info);
		}

		// Token: 0x06001135 RID: 4405 RVA: 0x0006E4A0 File Offset: 0x0006C6A0
		public bool SetCoordinateInfo(CharDefine.CoordinateType type)
		{
			return this.coordinateInfo != null && this.coordinateInfo.SetInfo(type, this.clothesInfo);
		}

		// Token: 0x06001136 RID: 4406 RVA: 0x0006E4C4 File Offset: 0x0006C6C4
		public static bool CheckSBPRFile(string path)
		{
			CharSave_SexyBeachPR charSave_SexyBeachPR = new CharSave_SexyBeachPR(CharSave_SexyBeachPR.ConvertType.HoneySelect);
			string fileTag = charSave_SexyBeachPR.GetFileTag(path);
			return !("【PremiumResortCharaFemale】" != fileTag) || !("【PremiumResortCharaMale】" != fileTag);
		}

		// Token: 0x06001137 RID: 4407 RVA: 0x0006E504 File Offset: 0x0006C704
		public static int CheckSBPRFileWithSex(string path)
		{
			CharSave_SexyBeachPR charSave_SexyBeachPR = new CharSave_SexyBeachPR(CharSave_SexyBeachPR.ConvertType.HoneySelect);
			string fileTag = charSave_SexyBeachPR.GetFileTag(path);
			if ("【PremiumResortCharaFemale】" == fileTag)
			{
				return 1;
			}
			if ("【PremiumResortCharaMale】" == fileTag)
			{
				return 0;
			}
			return -1;
		}

		// Token: 0x06001138 RID: 4408
		public abstract bool LoadFromSBPR(BinaryReader br);

		// Token: 0x06001139 RID: 4409 RVA: 0x0006E548 File Offset: 0x0006C748
		public static int CheckHoneySelectCharaFile(string path)
		{
			if (!File.Exists(path))
			{
				return -1;
			}
			int result;
			using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
			{
				using (BinaryReader binaryReader = new BinaryReader(fileStream))
				{
					if (binaryReader.BaseStream.Length == 0L)
					{
						GlobalData.Debug_LogError("数据被破坏");
						result = -1;
					}
					else
					{
						long num = 0L;
						PngAssist.CheckPngData(binaryReader, ref num, true);
						if (binaryReader.BaseStream.Length - binaryReader.BaseStream.Position == 0L)
						{
							GlobalData.Debug_LogWarning("可能只是一个PNG文件");
							result = -1;
						}
						else
						{
							string b = binaryReader.ReadString();
							int num2 = binaryReader.ReadInt32();
							if ("【HoneySelectCharaMale】" == b)
							{
								if (num2 == 1)
								{
									result = 0;
								}
								else
								{
									result = 2;
								}
							}
							else if ("【HoneySelectCharaFemale】" == b)
							{
								if (num2 == 1)
								{
									result = 1;
								}
								else
								{
									result = 3;
								}
							}
							else
							{
								result = -1;
							}
						}
					}
				}
			}
			return result;
		}

		// Token: 0x0600113A RID: 4410 RVA: 0x0006E670 File Offset: 0x0006C870
		public bool Load(string path = "", bool noSetPNG = false, bool noLoadStatus = true)
		{
			if (!File.Exists(path))
			{
				return false;
			}
			bool result;
			using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
			{
				result = this.Load(fileStream, noSetPNG, noLoadStatus);
			}
			return result;
		}

		// Token: 0x0600113B RID: 4411 RVA: 0x0006E6C4 File Offset: 0x0006C8C4
		public bool Load(TextAsset ta, bool noSetPNG = false, bool noLoadStatus = true)
		{
			if (null == ta)
			{
				return false;
			}
			bool result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				memoryStream.Write(ta.bytes, 0, ta.bytes.Length);
				memoryStream.Seek(0L, SeekOrigin.Begin);
				result = this.Load(memoryStream, noSetPNG, noLoadStatus);
			}
			return result;
		}

		// Token: 0x0600113C RID: 4412 RVA: 0x0006E734 File Offset: 0x0006C934
		public bool Load(Stream st, bool noSetPNG = false, bool noLoadStatus = true)
		{
			bool result;
			using (BinaryReader binaryReader = new BinaryReader(st))
			{
				result = this.Load(binaryReader, noSetPNG, noLoadStatus);
			}
			return result;
		}

		// Token: 0x0600113D RID: 4413 RVA: 0x0006E778 File Offset: 0x0006C978
		public bool Load(BinaryReader reader, bool noSetPNG = false, bool noLoadStatus = true)
		{
			if (reader.BaseStream.Length == 0L)
			{
				GlobalData.Debug_LogError("数据被破坏");
				return false;
			}
			long num = 0L;
			if (noSetPNG)
			{
				PngAssist.CheckPngData(reader, ref num, true);
			}
			else
			{
				this.charaFilePNG = PngAssist.LoadPngData(reader);
			}
			if (reader.BaseStream.Length - reader.BaseStream.Position == 0L)
			{
				GlobalData.Debug_LogWarning("可能只是一个PNG文件");
				return false;
			}
			string a = reader.ReadString();
			if (a != this.CharaFileMark)
			{
				GlobalData.Debug_LogError("不同类型的文件");
				return false;
			}
			this.charaLoadFileVersion = reader.ReadInt32();
			if (this.charaLoadFileVersion > 2)
			{
				GlobalData.Debug_LogError("比可执行文件更新的角色文件");
				return false;
			}
			int num2 = reader.ReadInt32();
			BlockHeader[] array = new BlockHeader[num2];
			for (int i = 0; i < num2; i++)
			{
				array[i] = new BlockHeader();
				array[i].LoadHeader(reader);
			}
			int num3 = 0;
			long position = reader.BaseStream.Position;
			this.previewInfo.previewLoadVersion = array[num3].version;
			num3++;
			this.customInfo.customLoadVersion = array[num3].version;
			if (this.customInfo.customLoadVersion > this.customInfo.version)
			{
				GlobalData.Debug_LogError("比可执行文件更新的自定义信息。");
			}
			else
			{
				reader.BaseStream.Seek(position + array[num3].pos, SeekOrigin.Begin);
				byte[] data = reader.ReadBytes((int)array[num3].size);
				if (!this.customInfo.LoadBytes(data, array[num3].version))
				{
					GlobalData.Debug_LogError("没有成功读取自定义信息");
					return false;
				}
			}
			string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(this.charaFileName);
			if ("ill_Sitri" != fileNameWithoutExtension)
			{
				this.customInfo.isConcierge = false;
			}
			num3++;
			this.coordinateInfo.coordinateLoadVersion = array[num3].version;
			if (this.coordinateInfo.coordinateLoadVersion > this.coordinateInfo.version)
			{
				GlobalData.Debug_LogError("比可执行文件更新的搭配信息。");
			}
			else
			{
				reader.BaseStream.Seek(position + array[num3].pos, SeekOrigin.Begin);
				byte[] data2 = reader.ReadBytes((int)array[num3].size);
				if (!this.coordinateInfo.LoadBytes(data2, array[num3].version))
				{
					GlobalData.Debug_LogError("我没能读取到搭配信息");
					return false;
				}
			}
			num3++;
			num3++;
			long offset = position + array[num3].pos + array[num3].size;
			reader.BaseStream.Seek(offset, SeekOrigin.Begin);
			if (2 <= this.charaLoadFileVersion)
			{
				reader.BaseStream.Seek(32L, SeekOrigin.Current);
			}
			reader.BaseStream.Seek(8L, SeekOrigin.Current);
			reader.BaseStream.Seek(8L, SeekOrigin.Current);
			return true;
		}

		// Token: 0x0600113E RID: 4414 RVA: 0x0006EA5C File Offset: 0x0006CC5C
		public bool LoadBlockData<T>(T blockInfo, string path) where T : BlockControlBase
		{
			if (blockInfo == null)
			{
				return false;
			}
			if (!File.Exists(path))
			{
				return false;
			}
			this.blockFileName = Path.GetFileNameWithoutExtension(path);
			bool result;
			using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
			{
				result = this.LoadBlockData<T>(blockInfo, fileStream);
			}
			return result;
		}

		// Token: 0x0600113F RID: 4415 RVA: 0x0006EAC8 File Offset: 0x0006CCC8
		public bool LoadBlockData<T>(T blockInfo, TextAsset ta) where T : BlockControlBase
		{
			bool result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				memoryStream.Write(ta.bytes, 0, ta.bytes.Length);
				memoryStream.Seek(0L, SeekOrigin.Begin);
				result = this.LoadBlockData<T>(blockInfo, memoryStream);
			}
			return result;
		}

		// Token: 0x06001140 RID: 4416 RVA: 0x0006EB28 File Offset: 0x0006CD28
		public bool LoadBlockData<T>(T blockInfo, Stream st) where T : BlockControlBase
		{
			bool result;
			using (BinaryReader binaryReader = new BinaryReader(st))
			{
				result = this.LoadBlockData<T>(blockInfo, binaryReader);
			}
			return result;
		}

		// Token: 0x06001141 RID: 4417 RVA: 0x0006EB68 File Offset: 0x0006CD68
		public bool LoadBlockData<T>(T blockInfo, BinaryReader reader) where T : BlockControlBase
		{
			if (reader.BaseStream.Length == 0L)
			{
				GlobalData.Debug_LogError("数据被破坏。");
				return false;
			}
			long num = 0L;
			PngAssist.CheckPngData(reader, ref num, true);
			if (reader.BaseStream.Length - reader.BaseStream.Position == 0L)
			{
				GlobalData.Debug_LogWarning("可能只是一个PNG文件");
				return false;
			}
			string a = reader.ReadString();
			if (a != this.CharaFileMark)
			{
				GlobalData.Debug_LogError("不同类型的文件");
				return false;
			}
			this.charaLoadFileVersion = reader.ReadInt32();
			if (this.charaLoadFileVersion > 2)
			{
				GlobalData.Debug_LogError("比可执行文件更新的角色文件");
				return false;
			}
			int num2 = reader.ReadInt32();
			BlockHeader[] array = new BlockHeader[num2];
			for (int i = 0; i < num2; i++)
			{
				array[i] = new BlockHeader();
				array[i].LoadHeader(reader);
			}
			long position = reader.BaseStream.Position;
			int num3 = -1;
			for (int j = 0; j < array.Length; j++)
			{
				if (array[j].tagName.StartsWith(blockInfo.tagName))
				{
					num3 = j;
					break;
				}
			}
			if (num3 == -1)
			{
				return false;
			}
			int version = array[num3].version;
			if (version > blockInfo.version)
			{
				GlobalData.Debug_LogError("比可执行文件更新的信息。");
				return false;
			}
			reader.BaseStream.Seek(position + array[num3].pos, SeekOrigin.Begin);
			byte[] data = reader.ReadBytes((int)array[num3].size);
			blockInfo.LoadBytes(data, version);
			if ("ill_Sitri" != this.blockFileName)
			{
				if (blockInfo is CharFileInfoPreview)
				{
					CharFileInfoPreview charFileInfoPreview = blockInfo as CharFileInfoPreview;
					charFileInfoPreview.isConcierge = 0;
				}
				else if (blockInfo is CharFileInfoCustomFemale)
				{
					CharFileInfoCustomFemale charFileInfoCustomFemale = blockInfo as CharFileInfoCustomFemale;
					charFileInfoCustomFemale.isConcierge = false;
				}
			}
			return true;
		}

		// Token: 0x06001142 RID: 4418 RVA: 0x0006ED70 File Offset: 0x0006CF70
		public static float ClampEx(float value, float min, float max)
		{
			return Mathf.Clamp(value, min, max);
		}

		// Token: 0x040012C5 RID: 4805
		public readonly string CharaFileMark = string.Empty;

		// Token: 0x040012C6 RID: 4806
		public readonly string CharaFileDirectory = string.Empty;

		// Token: 0x040012C7 RID: 4807
		public const int CharaFileVersion = 2;

		// Token: 0x040012C8 RID: 4808
		public byte[] charaFilePNG;

		// Token: 0x040012C9 RID: 4809
		public string charaFileName = string.Empty;

		// Token: 0x040012CA RID: 4810
		public int charaLoadFileVersion;

		// Token: 0x040012CB RID: 4811
		public string blockFileName = string.Empty;
	}
}
